package com.reflex.method;

import java.lang.reflect.Constructor;

/**
 * @Author: lizehui
 * @Description: 通过反射机制创建实例
 * @Date: 2022/11/21/13:05
 */
public class ReflectionInstance {
    public static void main(String[]args) throws Exception{
        //1、获取Person类的Class对象
        Class cls = Class.forName("com.reflex.dao.User");

        //2、获取public无参构造器
        Object o = cls.newInstance();
        System.out.println(o);
        //3、获取public有参构造器
        Constructor constructor = cls.getConstructor(String.class);
        Object o1 = constructor.newInstance("public new 李泽辉");
        System.out.println(o1);
        //4、获取private有参构造器
        Constructor constructor1 = cls.getDeclaredConstructor(String.class, int.class);
        //这里爆破之后，才能实例化私有的
        constructor1.setAccessible(true);
        Object o2 = constructor1.newInstance("private new 李泽辉", 100);
        System.out.println(o2);
    }



}
